<?php

/**
 * 
 * 首页格子模型
 */
class DazheModel extends Model {

    private $article, $link;

    public function _initialize() {
        $this->article = M('PayPage');
        $this->link = D('PayLink');
    }

    public function fabu($param) {
        $uid  = $param['uid'] ? $param['uid'] : session('uid');
        $shop = M('Shop')->where(array('user_id' => $uid))->find();
        if (!$shop) {
            return reStatus(false, '用户未登录或者非商家会员');
        }
        M()->startTrans();
        $data = array(
            'user_id'            => $uid,
            'title'              => $param['title'],
            'img_url'            => $param['main_img_url'],
            'description'        => $param['description'],
            'address'            => $param['address'],
            'star_time'          => $param['star_time'] ? strtotime($param['star_time']) : 0,
            'end_time'           => $param['star_time'] ? strtotime($param['star_time']) : 0,
            'days'               => $param['days'],
            'keywords'           => $param['keywords'],
            'index_top_days'     => $param['index_top_status'] ? $param['index_top_days'] : 0,
            'index_top_startime' => $param['index_top_status'] ? time() : 0,
            'index_top_endtime'  => $param['index_top_status'] ? time() + 24 * 3600 * $param['index_top_days'] : 0,
            'index_top_status'   => $param['index_top_status'] ? $param['index_top_status'] : 0,
            'cat_top_days'       => $param['cat_top_status'] ? $param['cat_top_days'] : 0,
            'cat_top_startime'   => $param['cat_top_status'] ? time() : 0,
            'cat_top_endtime'    => $param['cat_top_status'] ? time() + 24 * 3600 * $param['cat_top_days'] : 0,
            'cat_top_status'     => $param['cat_top_status'] ? $param['cat_top_status'] : 0,
            'cat_id'             => $param['cat_id'],
            'area_id'            => $param['area_id'],
            'shop_id'            => $shop['shop_id'],
            'phone'              => $param['phone'],
            'create_time'        => time(),
            'show_end_time'      => time() + 24 * 60 * 60 * $param['days'],
            'main_img_url'       => $param['main_img_url'],
            'detail_info'        => $param['detail_info'],
            'view_count'         => $param['view_count'] ? $param['view_count'] : 0,
            'pay_score'          => $param['pay_score'],
            'status'             => 1,
        );
        $decScore            = D('UserAccount')->chargeUserAccount(session(uid), -$param['pay_score'], 'FABU_DAZHE', '发布打折信息扣除金币' . $param['pay_score']);
        if ($decScore['status'] == true) {
            $dazheID = $this->add($data);
            if ($dazheID) {
                M()->commit();
                return reStatus(true, '发布优惠信息成功', $dazheID);
            } else {
                M()->rollback();
                return reStatus(false, '： 缺少必要参数');
            }
        } else {
            M()->rollback();
            return reStatus(false, ($decScore['status'] == false) ? $decScore['msg'] : '');
        }
    }

}

?>
